#include "datasheet.h"
unsigned short int DataSheet::tableData[241][13][100000] = {0};
DataSheet::DataSheet(QWidget *parent) : QTableWidget(parent)
{
    this->setRowCount(2*Height);
    this->setColumnCount(13);
    for (int i=0;i<2*Height;i++)
        this->setRowHeight(i,25);
    for (int i=0;i<10;i++)
        this->setColumnWidth(i,40);
    this->setColumnWidth(10,60);
    this->setColumnWidth(11,60);
    this->setColumnWidth(12,60);
    QStringList list1;
    for (int i=1;i<=10;i++)
        list1<<QString::number(i);
    list1 <<"left"<<"right"<<"middle";
    this->setHorizontalHeaderLabels(list1);
    QStringList list2;
    for (int i=1;i<=Height;i++)
    {
        list2<<QString::number(i)+" left";
        list2<<QString::number(i)+"right";
        this->setSpan(2*(i-1),10,2,1);
        this->setSpan(2*(i-1),11,2,1);
        this->setSpan(2*(i-1),12,2,1);
    }
    this->setVerticalHeaderLabels(list2);
    for (int i=0;i<2*Height;i++)
        for (int j=0;j<13;j++)
        {
            dataItem[i][j] = new QTableWidgetItem("");
            this->setItem(i,j,dataItem[i][j]);
        }
    this->setEditTriggers(QAbstractItemView::NoEditTriggers);
    //右键菜单
    copyMenu = new QAction("复制",this);
    this->addAction(copyMenu);
    this->setContextMenuPolicy(Qt::ActionsContextMenu);
    connect(copyMenu,SIGNAL(triggered(bool)),this,SLOT(copy()));
    clicpboard = QApplication::clipboard();
}
void DataSheet::copy()
{
    copyRange = this->selectedRanges();
    QTableWidgetSelectionRange range;
    int startx,starty,endx,endy;
    range = copyRange.takeFirst();
    startx = range.topRow();
    starty = range.leftColumn();
    while (!copyRange.empty())
        range = copyRange.takeFirst();
    endx = range.bottomRow();
    endy = range.rightColumn();
    QString s("");
    for (int i=startx;i<=endx;i++)
    {
        for (int j=starty;j<=endy;j++)
        {
            s+=dataItem[i][j]->text();
            s+=QString("\t");
        }
        s+="\n";
    }
    clicpboard->setText(s);
}
